<?php

namespace Rblog\Module\Index\Model;

class Sort extends \Rexzy\App\Model  {

    protected $_table = 'artsort';

    
    /**
     * 查询父类为$parent的分类的所有信息
     * @param int $parent 查询条件值（父类为 $parent的分类）
     * @return array 查询结果
     */
    public function navSort( $parent ) {
        $where = array( 'parent'=> $parent );
        $result = $this->selectAll( '*' ,$where, 5 );
        return $result;
    }
    
    /**
     * 查询符合条件的分类的id和名字
     * 
     * @param array()  $meg 查询条件
     * @return array() 查询结果
     */
    public function getSortMesg( $mesg ) {
        $result = $this->select( 'sortid, name', $mesg );
        return $result;
    }
    
    /**
     * 查询某个分类的所有子分类
     * 
     * @param int $sortid 查询分类的id
     * @return array() 
     */
    public function getChildren( $sortid ) {
        $allChildren = array();
        function get( $id, $data, $allChildren) {
            if ( $data->select( 'children', array( 'sortid' => $id ) )['children'] != 0 ) {
                $children = $data->selectAll( 'sortid', array( 'parent' => $id ) );
                foreach ( $children as $key => $val ) {
                    $allChildren[] = $val['sortid'];
                    if ( $data->select( 'children', array( 'sortid' => $val['sortid'] ) )['children'] > 0 ) {
                        $allChildren = get( $val['sortid'], $data, $allChildren );
                    }
                }
            }
            return $allChildren;
        };
        
        return get( $sortid, $this, $allChildren );
    }
    
    /**
     * 查询某个分类的最终的父分类
     * 
     * @param int $sortid 查询的分类的id
     * @return string 返回父分类的简称
     */
    public function findParent( $sortid ) {
        function find( $id, $data ) {
            $parent = $data->select( 'parent', array( 'sortid' => $id ) )['parent'];
            if ( $parent == 0 ) {
                return $data->select( 'slugname', array( 'sortid' => $id ) )['slugname'];
            } else {
                return find( $parent, $data );
            }
        }
       
        return find( $sortid, $this );
    }
    
    /**
     * 获取所有分类
     * 
     * @return array()
     */
    public function getAllSort() {
        
        function getsort( $parent, $data ) {
            $allsort = array();
            $sorts = $data->selectAll( '*', array( 'parent' => $parent ) );
            foreach( $sorts as $key => $val ) {
                if ( $val['children'] > 0 ) {
                    $sorts[$key]['children'] = array();
                    $sorts[$key]['children'] = getsort( $val['sortid'], $data, $sorts[$key]['children'] );
                }
                $allsort[$key] = $sorts[$key];
            }
            return $allsort;
        }
        
        $allsort = getsort( 0, $this );
        return $allsort;
    }
    
    /**
     * 查询某篇文章的分类信息
     * 
     * @param array() $art 包含文章信息的数组 
     * @return array() 包含文章所属分类的分类id，分类名，分类简称的一个数组
     */
    public function getSortName( $art ) {
        $sorts = explode( ',', $art['sort'] );
        $sortname = array();
        $sortid = array();
        $sortSlugname = array();
        foreach ( $sorts as $key => $val ) {
            $sortmesg = $this->select( 'name, slugname', array( 'sortid' => $val ) );
            $sortid[$key] = $val;
            $sortname[$key] = $sortmesg['name'];
            $sortSlugname[$key] = $sortmesg['slugname'];
        }
        
        $art['sort'] = $sortid;
        $art['sortname'] = $sortname;
        $art['sortslugname'] = $sortSlugname;
        
        return $art;
    }
   
}
